#
# import time
# import threading
#
#
# def loop1():
#     print("start loop1 :   ",time.ctime())
#     time.sleep(3)
#     print("end loop1 :     ",time.ctime())
#
# def loop2():
#     print("stat loop2 :    ",time.ctime())
#     time.sleep(2)
#     print("End loop2 :    ",time.ctime())
#
# def main():
#     print("-------------main start :   ",time.ctime())
#
#
#     threading.Thread(target=loop1).start()
#     threading.Thread(target=loop2).start()
#     time.sleep(5)
#
#
#     print("------------------End Main      :",time.ctime())
#
#
# if __name__=='__main__':
#     main()
#
#
#













#
# from time import ctime
# import threading
# import  time
#
# def coding():
#     # for i in range(5):
#     #     print('I\'m coding ','language', ' program at ', ctime() )
#     print('start test1   ',ctime())
#     time.sleep(2)
#     print('End test1   ',ctime())
#
# def music():
#     # for i in range(5):
#     #     print('I\'m listening music at ', ctime())
#     print('start test2   ',ctime())
#     time.sleep(2)
#     print('End test2   ',ctime())
#
#
#
#
#
# if __name__ == '__main__':
#
#     print('thread %s is running...' % threading.current_thread().name)
#
#     thread1_list = []
#     t1 = threading.Thread(target=coding)
#     t2 = threading.Thread(target=music)
#     thread1_list.append(t1)
#     thread1_list.append(t2)
#
#     for t in thread1_list:
#         t.setDaemon(True)  # 设置为守护线程
#         t.start()
#         # t.join()  # 在这个子线程完成运行之前，主线程将一直被阻塞
#     time.sleep(1.5)
#     print('thread %s ended.' % threading.current_thread().name)


#-*- encoding:UTF-8 -*-
import wx
import threading
import random


class WorkerThread(threading.Thread):
    """
    This just simulates some long-running task that periodically sends
    a message to the GUI thread.
    """
    def __init__(self, threadNum, window):
        threading.Thread.__init__(self)
        self.threadNum = threadNum
        self.window = window
        self.timeToQuit = threading.Event()
        self.timeToQuit.clear()
        self.messageCount = random.randint(10, 20)
        self.messageDelay = 0.1 + 2.0 * random.random()

    def stop(self):
        self.timeToQuit.set()

    def run(self):  # 运行一个线程
        msg = "Thread %d iterating %d times with a delay of %1.4f\n" \
              % (self.threadNum, self.messageCount, self.messageDelay)
        wx.CallAfter(self.window.LogMessage, msg)

        for i in range(1, self.messageCount + 1):
            self.timeToQuit.wait(self.messageDelay)
            if self.timeToQuit.isSet():
                break
            msg = "Message %d from thread %d\n" % (i, self.threadNum)
            wx.CallAfter(self.window.LogMessage, msg)
        else:
            wx.CallAfter(self.window.ThreadFinished, self)


class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Multi-threaded GUI")
        self.threads = []
        self.count = 0

        panel = wx.Panel(self)
        self.startBtn = wx.Button(panel, -1, "Start a thread",size=(100,30),pos=(50,20))
        self.stopBtn = wx.Button(panel, -1, "Stop all threads",size=(100,30),pos=(200,20))
        self.tc = wx.StaticText(panel, -1, "Worker Threads: 00",size=(100,30),pos=(120,80))
        self.log = wx.TextCtrl(panel, -1, "", style=wx.TE_RICH | wx.TE_MULTILINE,size=(300,300),pos=(40,110))


        self.Bind(wx.EVT_BUTTON, self.OnStartButton, self.startBtn)
        self.Bind(wx.EVT_BUTTON, self.OnStopButton, self.stopBtn)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

        self.UpdateCount()

    def OnStartButton(self, event):
        self.count += 1
        thread123 = WorkerThread(self.count, self)  # 创建一个线程
        self.threads.append(thread123)
        self.UpdateCount()
        thread123.start()  # 启动线程

    def OnStopButton(self, event):
        self.StopThreads()
        self.UpdateCount()

    def OnCloseWindow(self, event):
        self.StopThreads()
        self.Destroy()

    def StopThreads(self):  # 从池中删除线程
        while self.threads:
            thread = self.threads[0]
            thread.stop()
            self.threads.remove(thread)

    def UpdateCount(self):
        self.tc.SetLabel("Worker Threads: %d" % len(self.threads))

    def LogMessage(self, msg):  # 注册一个消息
        self.log.AppendText(msg)

    def ThreadFinished(self, thread):  # 删除线程
        self.threads.remove(thread)
        self.UpdateCount()


app = wx.App()
frm = MyFrame()
frm.Show()
app.MainLoop()






